package com.swf.common.i18n;

import java.util.*;
import java.util.Map.Entry;

import com.swf.common.base.*;
import com.swf.common.exception.*;

/**
 * description:语言资源控制中心
 * @version 1.0 2013-5-8
 * @author shenwf
 */
public class ResourceCenter {
	/**
	 * 存放资源的HashMap
	 */
	private Map<String,Resource> resourceHashMap;
	
	/**
	 *禁用默认构造函数 
	 */
	private ResourceCenter(){
	}
	
	/**
	 * 初始化构造器
	 * @param resourceFilePath
	 * @throws FwsException 
	 */
	public ResourceCenter(String resourceFilePath) throws FwsException{
		ResourceXMLReader reader = new ResourceXMLReader(resourceFilePath);
		this.resourceHashMap = reader.getResources();
	}
	public ResourceCenter(String resourceFilePath,String encoding) throws FwsException{
		ResourceXMLReader reader = new ResourceXMLReader(resourceFilePath,encoding);
		this.resourceHashMap = reader.getResources();
	}
	
	public String getResourceDisplayAt(String resourceName,String language) {
		Resource resource = resourceHashMap.get(resourceName);
		if (resource == null){
			return resourceName;
		}
		return resource.getValueAt(language);
	}
	
	@Override
	public String toString() {
		StringBuffer fileContent = new StringBuffer();
		fileContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append(FwsConstants.line_separator);
		fileContent.append("<resource.xml>");
		fileContent.append("<Resources>");
		for (Iterator<Entry<String, Resource>> iter = resourceHashMap.entrySet().iterator();iter.hasNext();){
			Entry<String, Resource> entry = iter.next();
			String resourceId = entry.getKey();
			Resource resource = resourceHashMap.get(resourceId);
			fileContent.append(resource);
		}
		fileContent.append("</Resources>");
		fileContent.append("</resource.xml>");
		return fileContent.toString();
	}
}
